<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/25
 * Time: 21:00
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class Follow
{
    public static function follow($dataArray){
        $follows = DB::table('weibo_follows');

        if(Follow::isFollowingS($follows,$dataArray) || Follow::isSelf($dataArray)){
            return false;
        }
        else{
            $result = $follows->insert($dataArray);
            if($result){
                return true;
            }
            else{
                return false;
            }
        }
    }

    public static function unfollow($dataArray){
        $follows = DB::table('weibo_follows');
        $isFollowing = $follows->where('followed_user_id','=',$dataArray['followed_user_id'])->where('following_user_id','=',$dataArray['following_user_id'])->count();
        if($isFollowing >= 1){
            $result = $follows->where($dataArray)->delete();
            if($result){
                return true;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }

    public static function isFollowingS($db,$dataArray){
        $result = $db->where('followed_user_id','=',$dataArray['followed_user_id'])->where('following_user_id','=',$dataArray['following_user_id'])->count();
        if($result >= 1){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isFollowing($dataArray){
        $follows = DB::table('weibo_follows');
        //var_dump($dataArray);
        $result = $follows->where('followed_user_id','=',$dataArray['followed_user_id'])->where('following_user_id','=',$dataArray['following_user_id'])->count();
        if($result >= 1){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isSelf($dataArray){
        if($dataArray['following_user_id'] == $dataArray['followed_user_id']){
            return true;
        }
        else{
            return false;
        }
    }

    public static function relationShip($dataArray){
        $follows = DB::table('weibo_follows');
        $result1 = $follows->where('followed_user_id','=',$dataArray['followed_user_id'])->where('following_user_id','=',$dataArray['following_user_id'])->count();
        $result2 = $follows->where('followed_user_id','=',$dataArray['following_user_id'])->where('following_user_id','=',$dataArray['followed_user_id'])->count();
        if ($result1 + $result2 == 0){
            return 0; //无关
        }
        else if($result1 == 1 && $result2 != 1){
            return 1; //你是他的粉丝
        }
        else if($result1 != 1 && $result2 == 1){
            return 2; //他是你的粉丝
        }
        else{
            return 3; //互粉
        }
    }
}